<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @clear="getcommentlist">
        <el-button slot="append" icon="el-icon-search" @click="getcommentlist"></el-button>
      </el-input>
      <!-- 文章表格 -->
      <el-table :data="commentlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="用户昵称" prop="uid.username"></el-table-column>
        <el-table-column label="文章标题" prop="aid.title"></el-table-column>
        <el-table-column label="评论时间" prop="time" width="180px">
          <template slot-scope="scope">
            {{scope.row.time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="评论内容" prop="content" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="点赞量" prop="likes"></el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="lookupReviews(scope.row._id)">查看</el-button>
            <el-button type="danger" size="mini" @click="removeComment(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      commentlist: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
        query: ''
      },
      total: 0
    }
  },
  created () {
    this.getcommentlist()
  },
  methods: {
    async getcommentlist () {
      const { data: res } = await this.$http.get('/comment', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取博文列表失败')
      console.log(res)
      this.commentlist = res.data.comments
      this.total = res.data.total
    },
    lookupReviews () {},
    async removeComment (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete('comment/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除评论失败')
      this.$message.success('删除评论成功')
      this.getcommentlist()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getcommentlist()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getcommentlist()
    }
  }
}
</script>
<style lang="less" scoped>
.el-input {
    width: 50%;
}
</style>
